package com.yanqu.road.server.manger.activity.goldingotfund;

import com.yanqu.road.dao.impl.activity.goldingotfund.GoldIngotFundDaoImpl;
import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.entity.activity.ActivityDetail;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.UserActivityConditionData;
import com.yanqu.road.entity.activity.goldingotfund.data.GoldIngotFundUserData;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.enums.eMailType;
import com.yanqu.road.entity.language.LanguageType;
import com.yanqu.road.entity.player.UserInfo;
import com.yanqu.road.logic.bussiness.player.UserActivityBussiness;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.logic.activity.condition.normal.RechargeVipDayCondition;
import com.yanqu.road.server.logic.mail.MailManager;
import com.yanqu.road.server.manger.MultipleLanguageMgr;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.log.LogMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.math.BigInteger;
import java.util.*;

public class GoldIngotFundMgr extends TempMgr {

    private static Logger logger = LogManager.getLogger(GoldIngotFundMgr.class.getName());

    @Override
    public boolean init() throws Exception {
        return true;
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }

    /**
     * 充值档补发
     * @param activityDetail
     * @param activityInfo
     */
    public static void autoSendReward(ActivityDetail activityDetail, ActivityInfo activityInfo) {
        //查询出有充值的玩家
        GoldIngotFundDaoImpl dao = new GoldIngotFundDaoImpl();
        Map<Long, GoldIngotFundUserData> rechargeUserMap = dao.getRechargeUserDataMap(activityDetail.getActivityId());
        //查询condition
        List<UserActivityConditionData> allConditionData = UserActivityBussiness.getUserActivityConditionDataListByType(activityDetail.getActivityId(), eGamePlayerEventType.GoldIngotFund.getValue());
        Map<Long, List<UserActivityConditionData>> userConditionData = new HashMap<>();
        for (UserActivityConditionData conditionData : allConditionData) {
            List<UserActivityConditionData> userConditionList =  userConditionData.computeIfAbsent(conditionData.getUserId(), k -> new ArrayList<>());
            userConditionList.add(conditionData);
        }
        int day = DateHelper.calc2DateTDOADays(new Date(activityInfo.getBeginTime() * 1000), new Date());
        //遍历玩家
        boolean isAllSend = true;
        for (Long userId : userConditionData.keySet()) {
            Property reward = new Property();
            for (UserActivityConditionData conditionData : userConditionData.get(userId)) {
                //已经领奖
                if (conditionData.isGetReward()) {
                    continue;
                }
                //是否有充值
                GoldIngotFundUserData goldIngotFundUserData = rechargeUserMap.get(userId);
                if (goldIngotFundUserData == null) {
                    continue;
                }
                ActivityConditionInfo activityConditionInfo = NormalActivityMgr.getActivityConditionInfo(activityDetail.getActivityId(), conditionData.getConditionId());
                if (activityConditionInfo == null) {
                    logger.error("auto send activity reward conditionInfo is null, activityId:{},userId:{},conditionId:{}", activityDetail.getActivityId(), conditionData.getUserId(), conditionData.getConditionId());
                    continue;
                }
                int conditionId = activityConditionInfo.getConditionId();
                List<BigInteger> paramList = activityConditionInfo.getParamList();
                int mallKeyId = paramList.get(0).intValue();
                //过滤掉非充值档
                if (mallKeyId == 0) {
                    continue;
                }
                //档位是否已经充值
                if (!goldIngotFundUserData.getBuyMallKeyIds().contains(mallKeyId)) {
                    continue;
                }
                //注入任务值
                conditionData.setValue(BigInteger.valueOf(day));
                //判断是否达成
                if (conditionData.getValue().compareTo(activityConditionInfo.getParamList().get(3)) < 0) {
                    continue;
                }
                conditionData.setGetReward(true);
                if (!UserActivityBussiness.updateUserActivityConditionData(conditionData)) {
                    isAllSend = false;
                    logger.error("auto send activity reward save db fail, activityId:{},userId:{},conditionId:{}", activityDetail.getActivityId(), conditionData.getUserId(), conditionData.getConditionId());
                    continue;
                }
                reward.addProperty(PropertyHelper.parseStringToProperty(activityConditionInfo.getRewardStr()));
                LogMgr.addLogActivityReward(userId, activityInfo.getActivityId(), activityInfo.getType(), activityInfo.getChildType(),
                        conditionId, 0, true, activityConditionInfo.getRewardStr());
            }

            if (reward.isNothing()) {
                continue;
            }

            UserInfo userInfo = UserMgr.getUserInfo(userId);
            String language = LanguageType.LANGUAGE_DEFAULT;
            if (null != userInfo) {
                language = userInfo.getLanguage();
            }
            String activityName = ActivityMgr.getActivityName(activityInfo, language);
            //{0}活动进度未领取奖励发放
            //亲爱的掌柜，您在 {0}活动中还有未领取的进度奖励，现已补发，敬请查收。
            String mailTitle = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.TREASURE_ACTIVITY_PROGRESS_REWORD_TITLE, language), activityName);
            String mailContent = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.TREASURE_ACTIVITY_PROGRESS_REWORD_CONTENT, language), activityName);
            MailManager.sendMail(userId, eMailType.SYSTEM.getValue(), PropertyHelper.parsePropertyToString(reward), mailContent, mailTitle);
        }
        if (isAllSend) {//更新自动发放标识
            activityDetail.setAutoSendRewardState(1);
        }
    }
}
